/**
* 动态页
* path:'/' 或 '/dynamic'
*/
<template>
  <div id="dynamic" class="rock-ref situation situation-main" data="1">
    <header-top :option="headerOption"></header-top>
    <div v-if="false">isLogin:{{isLogin}}, userCode: {{userCode}}, nickname: {{nickname}}</div>
    <div class="login" v-if="!isLogin" @click="login()">
      <img id="loginImg" src="../assets/images/beijing-wdl.png" width="100%">
    </div>
    <overview v-if="isLogin"></overview>
    <div class="wave">
      <img src="~assets/images/bowen@3x.png" width="100%" height="40px">
    </div>
    <rockmans-sub v-if="isLogin && manList.length>0" :manList="manList" :error="error"></rockmans-sub>
    <games-join v-if="isLogin && manList.length>0"></games-join>
    <games-create v-if="isLogin && manList.length>0"></games-create>
    <rockmans-show v-if="!isLogin || manList.length == 0"></rockmans-show>
    <div class="picture jxng" @click.stop.prevent="toJXNG">
      <img id="jxng" :src="bannerPic1"/>
      <div class="content-title">{{contentTitle1}}</div>
    </div>
    <groups-show v-if="!isLogin || manList.length == 0"></groups-show>
    <games-official v-if="!isLogin || manList.length == 0"></games-official>
    <div class="picture lhfp" @click.stop.prevent="toLHFP">
      <img id="lhfp" :src="bannerPic2"/>
      <div class="content-title">{{contentTitle2}}</div>
    </div>
  </div>
</template>

<script>
  import HeaderTop from 'components/HeaderTop'
  import OverView from 'components/dynamic/OverView'
  import RockmansSub from 'components/dynamic/RockmansSub'
  import RockmansShow from 'components/dynamic/RockmansShow'
  import GroupsShow from 'components/dynamic/GroupsShow'
  import GamesJoin from 'components/dynamic/GamesJoin'
  import GamesCreate from 'components/dynamic/GamesCreate'
  import GamesOfficial from 'components/dynamic/GamesOfficial'
  import Constant from 'common/constant'
  import RemoteDAO from 'common/remoteDAO'
  import Login from 'common/login'
  import LocalDAO from 'common/localDAO'
  import CommonUtil from 'common/common.util'
  import $ from 'jquery'

  export default {
    name: 'Dynamic',
    data () {
      return {
        headerOption: {
          title: '牛人牛股',
          leftBtn: Constant.leftBtn.CLOSE,
          operations: [Constant.operation.SEARCH]
        },
        manList:[],//获取的订阅牛列表
        error:'', //获取订阅牛人时的报错信息
        bannerPic1: '',
        bannerPic2: '',
        contentTitle1: '',
        contentTitle2: '',
        templetContentUrl1: '',
        templetContentUrl2: '',
        userCode: LocalDAO.getUserCode(),
        nickname: LocalDAO.getNickname(),
        isLogin: LocalDAO.getNickname() !== '' && LocalDAO.getNickname() !== null && typeof (LocalDAO.getNickname()) !== 'undefined'
      }
    },
    created: function () {
      this.jingXuanNiuGu()
      this.laoHeFuPan()
      this.initLogin()
    },
    components: {
      'header-top': HeaderTop,
      'overview': OverView,
      'rockmans-sub': RockmansSub,
      'rockmans-show': RockmansShow,
      'groups-show': GroupsShow,
      'games-join': GamesJoin,
      'games-create': GamesCreate,
      'games-official': GamesOfficial,
    },
    methods: {
      //初始化验证
      initLogin(){
        if (sessionStorage.getItem(Constant.flag.FIRST_IN) !== '1') {
          Login._init(()=> {
            this.nickname = LocalDAO.getNickname();
            if (typeof(this.nickname) === 'undefined' || this.nickname == '' || this.nickname == null) {
              this.isLogin = false;
            } else {
              this.isLogin = true;
            }
            sessionStorage.setItem(Constant.flag.FIRST_IN, '1');
          });
        }

        if(this.isLogin){
          this.fetchRockMansSub(); //查看订阅的牛人
        }

        this.$nextTick(()=>{
          if (!this.isLogin) {
            this.$store.dispatch('setGuide', {guideType: Constant.tutorial.MENU});
          } else {
            this.$store.dispatch('setGuide', {guideType: Constant.tutorial.USER_HEAD});
          }
        })

      },
      //登录
      login(){
        RemoteDAO.callNativeLogin((response)=> {
          if(response.error == 0){
//            window.location.reload(true);
            this.nickname = LocalDAO.getNickname();
            this.usercode = LocalDAO.getUserCode();
            if (typeof(this.nickname) === 'undefined' || this.nickname == '' || this.nickname == null) {
              this.isLogin = false;
            } else {
              this.isLogin = true;
              this.fetchRockMansSub(); //查看订阅的牛人
              this.$store.dispatch('setGuide', {guideType: Constant.tutorial.USER_HEAD});
            }
          }else{
            this.$store.dispatch('setTip',{text:response.data});
          }
        })
        // 埋点
        var object = {};
        object.usercode = this.userCode;
        TDAPP.onEvent("2001", "", object);
        TDAPP.send();
      },
      jingXuanNiuGu(){ // 精选牛股
        let self = this
        $.ajax({
          type: 'GET',
          url: 'https://dl.app.gtja.com/operation/config/niurenniugu/jingxuanniugu.json',
          dataType: 'jsonp',
          jsonp: 'jingxuanniugu',
          success: function (o) {
          }
        })
        function jingxuanniugu(response) {
          self.bannerPic1 = response.jingxuanniugu[0].bannerPic
          self.contentTitle1 = response.jingxuanniugu[0].contentTitle
          self.templetContentUrl1 = response.jingxuanniugu[0].templetContentUrl
        }

        window.jingxuanniugu = jingxuanniugu
      },
      laoHeFuPan(){ // 老何复盘
        let self = this
        $.ajax({
          type: 'GET',
          url: 'https://dl.app.gtja.com/operation/config/niurenniugu/laohefupan.json',
          dataType: 'jsonp',
          jsonp: 'laohefupan',
          success: function (o) {
          }
        })
        function laohefupan(response) {
          self.bannerPic2 = response.laohefupan[0].bannerPic
          self.contentTitle2 = response.laohefupan[0].contentTitle
          self.templetContentUrl2 = response.laohefupan[0].templetContentUrl
        }

        window.laohefupan = laohefupan
      },
      //跳转精选牛股页面
      toJXNG(){
        this.$router.push({path: '/selectionjxng', query: {templetContentUrl: this.templetContentUrl1}});
        // 埋点
        var object = {};
        object.usercode = this.userCode;
        TDAPP.onEvent("10001", "", object);
        TDAPP.send();
      },
      //跳转老何复盘页面
      toLHFP(){
        this.$router.push({path: '/selectionlhfp', query: {templetContentUrl: this.templetContentUrl2}});
        // 埋点
        var object = {};
        object.usercode = this.userCode;
        TDAPP.onEvent("9001", "", object);
        TDAPP.send();
      },
      //获取我订阅的牛人（登录时）
      fetchRockMansSub () {
        let userCode = LocalDAO.getUserCode();
        this.error = '数据加载中...';
        // 获取 3 条订阅的牛人数据
        RemoteDAO.callUC12008301(userCode, '1', '3', '1', '0', (response)=> {
          let manList = [];
          let userCodes = [];
          if (Number(response.error) === 0) {
            if(response.data.body.length>0){
              _.each(response.data.body, (item, index)=>{
                let object = {};
                object.userCode = item.USER_CODE; // 牛人代码
                object.nickname = item.NICKNAME; // 牛人昵称
                object.userHead = item.USER_HEAD == '' ? CommonUtil.getDefaultUserHead(item.USER_CODE) : item.USER_HEAD; // 牛人头像
                object.action = '-'; // 操作
                object.portfolioName = '-'; // 组合名称
                object.stockName = '-'; // 股票名称
                object.stockCode = '-'; // 股票代码
                object.costPrice = '-'; // 成交价
                object.datetime = '-'; // 操作时间
                object.subscribeFlag = item.SUBSCRIBE_FLAG; // 订阅标注
                userCodes.push(item.USER_CODE);
                manList.push(object);
              });
              //批量查询牛人最近操作
              RemoteDAO.callJCL11139(userCodes, userCode, (resp) =>{
                if (resp.error == 0) {
                  _.each(resp.data.body.rockman, (item, index)=>{
                    _.each(manList, (man, index)=> {
                      if (man.userCode == item.client_id) {
                        man.operationDirection = item.operation_direction; // 操作
                        man.operationDirectionName = Constant.OPERATION_DIRECTION[item.operation_direction]; // 操作
                        man.operationName = item.operation_name; // 组合名称
                        man.stockCode = item.stock_code; // 股票代码
                        man.operationPrice = CommonUtil.toFixed(item.operation_price, 2); // 成交价
                        man.operationVol = item.operation_vol; // 最近操作数量
                        man.operationDate = item.operation_date == CommonUtil.getTodayFormatDate() ? "今天" : CommonUtil.formatDatePoint(item.operation_date);// 操作时间
                        let position = man.operationDate.indexOf(".");
                        if (position >= 0) { // 只取月、日
                          man.operationDate = man.operationDate.substring(position + 1);
                        }
                        man.operationTime = CommonUtil.formatHourMinute(item.operation_time); // 操作时间
                      }
                    });
                  });
                  this.manList = manList;
                  this.error = '';
                } else {
                  this.manList = manList;
                  this.error = resp.data
                }
                LocalDAO.setRockmansSub({list: manList});
              });
            }else{
              this.manList = [];
              this.error = '';
            }
          } else {
            this.error = response.data
          }
        })
      },
    }
  }
</script>

